package com.datatools.ui.sqledit;

import com.datatools.ui.dataview.DataGrid;
import com.datatools.ui.frame.LeftPanel;
import com.datatools.ui.frame.LeftPanelTreeNode;

import javax.swing.*;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.StyleContext;
import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.io.Serializable;
import java.util.List;

/**
 * SQL 编辑父容器
 */
public class SQLEditPanel extends JSplitPane implements Serializable {
    private static final long serialVersionUID = 1L;
    //ToolBar
    private SQLEditToolBar sqlEditToolBar;

    //SQL编辑器
    private SQLEdit sqlEdit;

    //数据展示表格
    private DataGrid dataGrid;

    //信息展示面板
    private JTextPane textPane;

    public SQLEditPanel() {
        //让分割线显示出箭头
        this.setOneTouchExpandable(true);
        //操作箭头，重绘图形
        this.setContinuousLayout(true);
        //设置分割线方向 ,上下分割
        this.setOrientation(JSplitPane.VERTICAL_SPLIT);
        //设置分割线的宽度
        this.setDividerSize(5);
        //设定分割线的距离上边的位置
        this.setDividerLocation(300);

        this.sqlEdit = new SQLEdit();
        this.dataGrid = new DataGrid();
        this.textPane = new JTextPane();

        //调整列宽时后面所有的列跟着调整
        this.dataGrid.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        //TODO 表格标题的列 事件
        this.dataGrid.getTableHeader().addComponentListener(new ComponentListener() {
            @Override
            public void componentResized(ComponentEvent e) {
                Dimension dimension = e.getComponent().getSize();
                System.out.println("componentResized");
            }

            @Override
            public void componentMoved(ComponentEvent e) {

            }

            @Override
            public void componentShown(ComponentEvent e) {

            }

            @Override
            public void componentHidden(ComponentEvent e) {

            }
        });

        //SQLEditToolbar
        List<LeftPanelTreeNode> allLevel1ConnectionInfoNodes = LeftPanel.getInstance().getAllLevel1ConnectionInfoNode();
        this.sqlEditToolBar = new SQLEditToolBar();
        this.sqlEditToolBar.setAllLevel1ConnectionInfoNodes(allLevel1ConnectionInfoNodes);

        sqlEditToolBar.buildGUI();
        //上部分容器
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new BorderLayout());
        //添加toolbar
        topPanel.add(sqlEditToolBar, BorderLayout.NORTH);
        //自动出现滚动条的 SQLEdit
        JScrollPane scrollEditPane = new JScrollPane();
        scrollEditPane.setViewportView(sqlEdit);
        scrollEditPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollEditPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        topPanel.add(scrollEditPane, BorderLayout.CENTER);

        //将上、下组件放入
        this.setTopComponent(topPanel);

        //下半部分容器
        setBottomDataGrid();

    }


    public void setBottomTextPanel(String text) {
        textPane.setText(text);
        this.setBottomComponent(textPane);
        this.repaint();
    }

    public void setBottomDataGrid() {
        //下半部分容器
        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(new BorderLayout());
        //数据的滚动面板
        JScrollPane scrollGridPane = new JScrollPane();
        scrollGridPane.setViewportView(dataGrid);
        scrollGridPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollGridPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        bottomPanel.add(scrollGridPane, BorderLayout.CENTER);
        this.setBottomComponent(bottomPanel);
        this.repaint();
    }

    public SQLEditToolBar getSqlEditToolBar() {
        return sqlEditToolBar;
    }

    public void setSqlEditToolBar(SQLEditToolBar sqlEditToolBar) {
        this.sqlEditToolBar = sqlEditToolBar;
    }

    public SQLEdit getSqlEdit() {
        return sqlEdit;
    }

    public void setSqlEdit(SQLEdit sqlEdit) {
        this.sqlEdit = sqlEdit;
    }

    public DataGrid getDataGrid() {
        return dataGrid;
    }

    public void setDataGrid(DataGrid dataGrid) {
        this.dataGrid = dataGrid;
    }

    public JTextPane getTextPane() {
        return textPane;
    }

    public void setTextPane(JTextPane textPane) {
        this.textPane = textPane;
    }
}
